// pkg_wo/pages/setPage/setPage.js

import request from '../../../utils/http.js'
const app = getApp()
var qqmapsdk;
// const chooseLocation = requirePlugin('chooseLocation');


Page({

  /**
   * 页面的初始数据
   */
  data: {
    winHeight:0,
    personId: 0,
    shopList: [],
    dianPuId: 0,

    latitude: 0,
    longitude: 0,
    markers: [],
    count: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      personId: options.personId,
      dianPuId: options.dianPuId,
    })

    this.getShopList();

    var that = this;
    //  高度自适应
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winHeight: res.windowHeight - (560 / 750 * res.windowWidth),
        });
      }
    });

  },

  //获取关联店铺List
  getShopList: function () {
    var that = this
    // console.log("that.data.personId:"+that.data.personId)
    let data = {
      personId: that.data.personId,
    }

    request.get('person/guanLianDianPu', data, res => {
      // console.log("店铺List:" + JSON.stringify(res.data))
      that.setData({
        shopList: res.data.list,
        markers: [],
      })


      if (Array.isArray(that.data.shopList) && that.data.shopList.length !== 0) {
        that.setData({
          count: that.data.shopList.length
        })

        that.data.shopList.forEach((item, index) => {
          var pointObj = {}
          pointObj.id = item.dianPuId
          pointObj.latitude = item.mapWeiDu,
            pointObj.longitude = item.mapJingDu,
            pointObj.width = 40, //图片显示宽度
            pointObj.height = 40, //图片显示高度
            pointObj.iconPath = '/pkg_wo/image/shopAddressNo.png',
            pointObj.title = item.mingCheng

          if (that.data.dianPuId == item.dianPuId) {
            pointObj.iconPath = '/pkg_wo/image/shopAddress.png'
            that.setData({
              latitude: item.mapWeiDu,
              longitude: item.mapJingDu,
            })
          } else {
            pointObj.iconPath = '/pkg_wo/image/shopAddressNo.png'
          }


          that.data.markers.push(pointObj)
        })


        that.setData({
          shopList: that.data.shopList,
          markers: that.data.markers
        })

        wx.setNavigationBarTitle({
          title: '关联服务站:' + that.data.count + '个'
        })

      }


    }, err => {
      wx.showToast({
        title: '获取店铺失败', // 提示的内容
        icon: "none", // 图标，默认success
        duration: 1000, // 提示的延迟时间，默认1500
        mask: false, // 是否显示透明蒙层，防止触摸穿透
      })
    })
  },

  radioChange(e) {


    var that = this

    let data = {
      personId: that.data.personId,
      defaultDianPuId: e.detail.value
    }

    request.get('person/setDefaultDianPu', data, res => {
      //  console.log("修改默认店铺:" + JSON.stringify(res.data))
      this.data.shopList.forEach(item => {
        if (item.dianPuId == e.detail.value) {
          item.isDefaultDianPu = 1      
        } else {
          item.isDefaultDianPu = 2
        }
  
      });
  
      this.setData({
        shopList: this.data.shopList
      })



    }, err => {
      wx.showToast({
        title: '获取店铺失败', // 提示的内容
        icon: "none", // 图标，默认success
        duration: 1000, // 提示的延迟时间，默认1500
        mask: false, // 是否显示透明蒙层，防止触摸穿透
      })
    })





  },

  changeDianPu: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.setStorageSync(app.globalData.SHOPID, id)
    app.globalData.shopIsChange = true

    let pages = getCurrentPages(); //定义上页面
    let currPage = null; //当前页面
    let prevPage = null; //上一个页面
    if (pages.length >= 2) {
      currPage = pages[pages.length - 1]; //当前页面   
      prevPage = pages[pages.length - 2]; //上一个页面        
      if (prevPage) {
        prevPage.setData({
          shopIsChange: true,
          dianPuId:id
        });
      }
    }

    var that=this

    that.data.markers.forEach(item => {
      // console.log(item.id+'==='+id)
      if (item.id == id) {
        item.iconPath = '/pkg_wo/image/shopAddress.png'
        that.setData({
          latitude: item.latitude,
          longitude: item.longitude,
        })
      } else {
           
          item.iconPath = '/pkg_wo/image/shopAddressNo.png'
      }

    });

    that.setData({
      dianPuId:id,
      markers:that.data.markers
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})